local filename = "../webservice/matamatajogos"
local imprime = false

function redrawTela1()    
    file, err = io.open(filename, "r")
    if (err == nil) then
        canvas:attrFont('vera',26,'bold')
        local i = 0
	local j = 0
        for line in file:lines() do
            --print ("****" .. line)
            time1, value1, value2, time2, pen1, pen2 = string.match(line, "^([a-z-]*)[ ]+([0-9]*)[ ]+([0-9]*)[ ]+([a-z-]*)[ ]*([0-9]*)[ ]*([0-9]*)$")
            matamataTela = string.match(line, "^([a-zA-Z12]*)$")
            if (matamataTela ~= nil and matamataTela == 'mataMata1') then imprime = true elseif (matamataTela ~= nil and matamataTela ~= tela) then imprime = false end
            if (imprime and time1 ~= nil) then
                local img1 = canvas:new(("../media/bandeiras-legendas/" .. time1 .. ".gif"))
                local img2 = canvas:new(("../media/bandeiras-legendas/" .. time2 .. ".gif"))
                if i < 4 then
                    canvas:compose(20, (12 + i*88 + j), img1)
                    canvas:compose(20, (12 + 46 + i*88 + j), img2)
                    canvas:drawText(142, (13 + i*88) + j, value1)
                    canvas:drawText(142, (13 + 46 + i*88 + j), value2)
                    j = j + 6
                elseif i > 3 and i < 6 then
                    canvas:compose(215, (12 + (i - 4)*175 + j), img1)
                    canvas:compose(215, (12 + 94 + (i - 4)*175 + j), img2)
                    canvas:drawText(339, (12 + (i - 4)*175 + j), value1)
                    canvas:drawText(339, (12 + 94 + (i - 4)*175 + j), value2)
                    j = j + 12
                elseif i < 7 then
                    canvas:compose(412, 12, img1)
                    canvas:compose(412, 197, img2)
                    canvas:drawText(534, 13, value1)
                    canvas:drawText(534, 200, value2)
                else
                    canvas:compose(565, 105, img1)
                    canvas:drawText(685, 105, value1)
                end
                i = i + 1
		if i == 4 then
			j = 0
		end
                canvas:flush()                    
            end
        end
        file:close()
    else 
        print (err)
    end
end

function redrawTela2()
    file, err = io.open(filename, "r")
    if (err == nil) then
        canvas:attrFont('vera',26,'bold')
        local i = 0
        for line in file:lines() do
            print ("****" .. line)
            time1, value1, value2, time2, pen1, pen2 = string.match(line, "^([a-z-]*)[ ]+([0-9]*)[ ]+([0-9]*)[ ]+([a-z-]*)[ ]*([0-9]*)[ ]*([0-9]*)$")
            matamataTela = string.match(line, "^([a-zA-Z12]*)$")
            if (matamataTela ~= nil and matamataTela == 'mataMata2') then imprime = true elseif (matamataTela ~= nil and matamataTela ~= tela) then imprime = false end
            if (imprime and time1 ~= nil) then
                local img1 = canvas:new(("../media/bandeiras-legendas/" .. time1 .. ".gif"))
                local img2 = canvas:new(("../media/bandeiras-legendas/" .. time2 .. ".gif"))
                if i < 4 then
                    canvas:compose(532, (29 + i*89), img1)
                    canvas:compose(532, (29 + 45 + i*89), img2)
                    canvas:drawText(493, (31 + i*89), value1)
                    canvas:drawText(493, (31 + 43 + i*89), value2)
                    if (pen1 ~= null and pen1 ~= "") then
                        canvas:attrFont('vera',20,'normal')
                        canvas:drawText(500, (31 + i*89), pen1)
                        canvas:drawText(500, (31 + 43 + i*89), pen2)
                        canvas:attrFont('vera',26,'bold')
                    end
                elseif i > 3 and i < 6 then
                    canvas:compose(376, (56 + (i - 4)*177), img1)
                    canvas:compose(376, (56 + 89 + (i - 4)*177), img2)
                    canvas:drawText(346, (56 + (i - 4)*175), value1)
                    canvas:drawText(346, (56 + 89 + (i - 4)*175), value2)
                    if (pen1 ~= null and pen1 ~= "") then
                        canvas:drawText(350, (54 + (i - 4)*175), pen1)
                        canvas:drawText(350, (54 + 89 + (i - 4)*175), pen2)
                    end
                elseif i < 7 then
                    canvas:compose(220, 146, img1)
                    canvas:compose(210, 233, img2)
                    canvas:drawText(210, 146, value1)
                    canvas:drawText(200, 233, value2)
                    if (pen1 ~= null and pen1 ~= "") then
                        canvas:drawText(230, 144, pen1)
                        canvas:drawText(230, 230, pen2)
                    end
                else
                    canvas:compose(54, 191, img1)
                    canvas:drawText(12, 191, value1)
                    if (pen1 ~= null and pen1 ~= "") then
                        canvas:drawText(10, 178, pen1)
                    end
                end
                i = i + 1
                canvas:flush()
            end
        end
        file:close()
    else 
        print (err)
    end
end

function handler(evt)
    print(evt.class, evt.type, evt.name, evt.action, evt.value, evt.label)
    if (evt.label ~= nil and evt.class == 'ncl' and evt.type == 'presentation' and evt.action == 'start') then
        if (evt.label == 'mataMata1') then
            redrawTela1()
        elseif (evt.label == 'mataMata2') then
            redrawTela2()
        end
    --elseif (evt.class == 'ncl' and evt.type == 'presentation' and evt.action == 'stop') then
    --    event.post('out', evt)
    end
    event.post('out', evt)
    --return
end

event.register(handler)
